﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using DunLibrary.Query;
using DunSilverlight.Comm;
using DunLibrary.Client;

namespace DunSilverlight.Views
{
    public partial class ImportArrearRecord : UserControl
    {
        

        public ImportArrearRecord()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ImportArrearRecord_Loaded);

            
        }

        void ImportArrearRecord_Loaded(object sender, RoutedEventArgs e)
        {


            cmbPeriods.ItemsSource = AppGlobal.Periods;

            try
            {
                Action<IList<Company>> getBanks=(IList<Company> banks)=>
                {
                    cmbBank.ItemsSource = banks;
                };

                AppGlobal.GetBanks(getBanks);

            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }
        }

        /// <summary>
        /// 文件上传完成后开始执行导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadUpload1_FileUploaded(object sender, Telerik.Windows.Controls.FileUploadedEventArgs e)
        {
            try
            {
                if (CheckImport())
                {
                    RadUploadSelectedFile files = e.SelectedFile;

                    string fileName = files.Name;

                    ImportService.ImportServiceClient ser = new ImportService.ImportServiceClient();

                    ser.ImportArrearRecordsCompleted += (object sender2, ImportService.ImportArrearRecordsCompletedEventArgs e2) =>
                        {
                            string result = e2.Result;
                            JsonResult r = null;
                            try
                            {
                                r = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonResult>(result);
                                Loger.WriteLog("导入", "导入催款委案,文件名:"+fileName);
                            }catch(Exception ex)
                            {
                                Tools.ShowMessage(ex.Message, "", false);
                            }
                            MessageBox.Show(r.Data + r.Message);
                        };

                    int year = int.Parse(txtYear.Text);
                    int period = (int)cmbPeriods.SelectedItem;
                    int bankId = ((Company)cmbBank.SelectedItem).Id;

                    DunLibrary.User.User user=AppGlobal.CurrentUser;

                    ser.ImportArrearRecordsAsync(uplArrear.TargetFolder + @"\" + fileName, bankId, year, period, user.Organization.Id, user.Id);
                }

            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }
        }

        private bool CheckImport()
        {
            int year=0;
            if(!int.TryParse(txtYear.Text,out year))
            {
                MessageBox.Show("必须输入有效的年分!");

                return false;
            }

            if(year==0)
            {
                MessageBox.Show("必须输入有效的年分!");
                return false;
            }

            object period = cmbPeriods.SelectedValue;

            if (period == null)
            {
                MessageBox.Show("请选择期间!");
                return false;
            }

            object bank = cmbBank.SelectedItem;

            if (bank == null)
            {
                MessageBox.Show("请选择银行!");

                return false;
            }

            return true;
        }


        private void RadUpload1_UploadStarted(object sender, Telerik.Windows.Controls.UploadStartedEventArgs e)
        {
             
        }

    }
}
